package com.seo.service.sysUser.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.seo.mapper.sysUser.SysUserMapper;
import com.seo.mapper.user.UserMapper;
import com.seo.pojo.ReturnBody;
import com.seo.service.sysUser.sdk.SysUserService;
import com.seo.utils.SerialCodeUtil;

import cn.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

	@Autowired
	private SysUserMapper sysUserMapper;

	@Autowired
	private UserMapper userMapper;

	@Transactional(readOnly = true)
	@Override
	public ReturnBody login(String loginName, String password, String code) {
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> sysUser = sysUserMapper.getSysUserByLoginName(loginName);
		if (sysUser == null) {
			return rb.error("error", "用户不存在");
		}

		String realPassword = (String) sysUser.get("password");

		String inputPassword = SecureUtil.md5(password);
		if (!inputPassword.equals(realPassword)) {
			return rb.error("error", "输入密码错误");
		}
		rb.put("sysUser", sysUser);

		return rb.success();
	}

	@Override
	public List<Map<String, Object>> getSysUserList(Map<String, Object> params) {
		return sysUserMapper.getSysUserList(params);
	}

	@Override
	public Map<String, Object> getSysUserById(Integer id) {
		return sysUserMapper.getSysUserById(id);
	}

	@Override
	public Integer getSysUserCnt(Map<String, Object> params) {
		return sysUserMapper.getSysUserCnt(params);
	}

	@Override
	public ReturnBody addSysUser(Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		try {
			if (!params.containsKey("roleId")) {
				return rb.error("请选择角色");
			}
			String roleId = (String) params.get("roleId");
			if (StringUtils.isEmpty(roleId)) {
				return rb.error("请选择角色");
			}

			if (!params.containsKey("loginName")) {
				return rb.error("请输入账号");
			}
			String loginName = (String) params.get("loginName");
			if (StringUtils.isEmpty(loginName)) {
				return rb.error("请输入账号");
			}

			if (!params.containsKey("name")) {
				return rb.error("请输入姓名");
			}
			String name = (String) params.get("name");
			if (StringUtils.isEmpty(name)) {
				return rb.error("请输入姓名");
			}

			if (!params.containsKey("password")) {
				return rb.error("请输入密码");
			}
			String password = (String) params.get("password");
			if (StringUtils.isEmpty(password)) {
				return rb.error("请输入密码");
			}

			if (!params.containsKey("phone")) {
				return rb.error("请输入手机号");
			}
			String phone = (String) params.get("phone");
			if (StringUtils.isEmpty(phone)) {
				return rb.error("请输入手机号");
			}

			params.put("password", SecureUtil.md5(password));
			params.put("userType", "00");
			params.put("state", "01");

			int num = sysUserMapper.addSysUser(params);
			if (num == 0) {
				return rb.rollback();
			}

			int id = (int) params.get("id");
			String inviteCode = SerialCodeUtil.toSerialCode(id);
			params.clear();
			params.put("inviteCode", inviteCode);
			params.put("id", id);
			num = sysUserMapper.updateSysUser(params);
			if (num == 0) {
				return rb.rollback();
			}

			return rb.success();
		} catch (Exception e) {
			log.error("异常-{}", e);
			return rb.rollback();
		} finally {
			rb.result();
		}

	}

	@Override
	public ReturnBody deleteSysUser(Integer id) {
		ReturnBody rb = ReturnBody.init();
		int num = sysUserMapper.deleteSysUser(id);
		if (num == 0) {
			return rb.error();
		}

		return rb.success();
	}

	@Override
	public ReturnBody updateSysUser(Map<String, Object> params) {

		ReturnBody rb = ReturnBody.init();
		try {
			if (!params.containsKey("id")) {
				return rb.error("用户不存在");
			}
			String id = (String) params.get("id");
			if (StringUtils.isEmpty(id)) {
				return rb.error("用户不存在");
			}

			if (params.containsKey("roleId")) {
				String roleId = (String) params.get("roleId");
				if (StringUtils.isEmpty(roleId)) {
					return rb.error("请选择角色");
				}
			}

			if (params.containsKey("loginName")) {
				String loginName = (String) params.get("loginName");
				if (StringUtils.isEmpty(loginName)) {
					return rb.error("请输入账号");
				}
			}

			if (params.containsKey("name")) {
				String name = (String) params.get("name");
				if (StringUtils.isEmpty(name)) {
					return rb.error("请输入姓名");
				}
			}

			if (params.containsKey("phone")) {
				String phone = (String) params.get("phone");
				if (StringUtils.isEmpty(phone)) {
					return rb.error("请输入手机号");
				}
			}

			if (params.containsKey("password")) {
				String password = (String) params.get("password");
				if (StringUtils.isEmpty(password)) {
					return rb.error("请输入密码");
				}
				params.put("password", SecureUtil.md5(password));
			}
			int num = sysUserMapper.updateSysUser(params);
			if (num == 0) {
				return rb.rollback();
			}
			return rb.success();
		} catch (Exception e) {
			log.error("异常-{}", e);
			return rb.rollback();
		} finally {
			rb.result();
		}

	}

	@Override
	public ReturnBody updateSysUserState(Integer id, String state) {

		ReturnBody rb = ReturnBody.init();
		int num = sysUserMapper.updateSysUserState(id, state);
		if (num == 0) {
			return rb.error();
		}

		return rb.success();
	}

	@Override
	public ReturnBody updateUserPwd(Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		if (!params.containsKey("oldPassword")) {
			return rb.error("请输入原始密码");
		}

		String oldPassword = (String) params.get("oldPassword");
		if (StringUtils.isEmpty(oldPassword)) {
			return rb.error("请输入原始密码");
		}

		if (!params.containsKey("password")) {
			return rb.error("请输入密码");
		}

		String password = (String) params.get("password");
		if (StringUtils.isEmpty(password)) {
			return rb.error("请输入密码");
		}

		if (!params.containsKey("confirmPassword")) {
			return rb.error("请再次确认密码");
		}

		String confirmPassword = (String) params.get("confirmPassword");
		if (StringUtils.isEmpty(confirmPassword)) {
			return rb.error("请再次确认密码");
		}

		if (!StringUtils.equals(confirmPassword, password)) {
			return rb.error("再次确认密码错误");
		}

		Integer id = (Integer) params.get("id");
		Map<String, Object> user = sysUserMapper.getSysUserById(id);
		if (user == null) {
			return rb.error("用户不存在");
		}
		String dataPassword = (String) user.get("password");
		if (!dataPassword.equals(SecureUtil.md5(oldPassword))) {
			return rb.error("原始密码不正确");
		}

		params.put("password", SecureUtil.md5(password));

		int num = sysUserMapper.updateSysUserPwd(params);
		if (num == 0) {
			return rb.error("重置密码失败");
		}
		return rb.success();
	}

	@Override
	public ReturnBody updateBelongName(Integer userId, String belongName) {
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> sysUser = sysUserMapper.getSysUserByLoginName(belongName);
		if (sysUser == null) {
			return rb.error("error", "归属用户不存在");
		}
		Integer belongId = (Integer) sysUser.get("id");
		Map<String, Object> params = new HashMap<>();
		params.put("id", userId);
		params.put("belongId", belongId);
		int num = userMapper.updateUser(params);
		if (num == 0) {
			return rb.error("更新归属失败");
		}
		return rb.success();
	}

}
